5.2.5 开机自启动配置
Video: https://www.bilibili.com/video/BV1rm4y1E73q/?p=15
Ubuntu系统添加自启动程序的方式有多种方法,本章节提供两种方法作为参考。
设置自启动Service
-
创建启动脚本
使用任何文本编辑器,在
/etc/init.d
目录下创建一个新的启动脚本,假设命名为your_script_name
,以下是示例脚本的参考内容:#!/bin/bash
### BEGIN INIT INFO
# Provides: your_service_name
# Required-Start: $all
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start your_service_name at boot time
# Description: Enable service provided by your_service_name
### END INIT INFO
/path/to/your/program &
exit 0 -
设置启动脚本具有可执行权限
sudo chmod +x /etc/init.d/your_script_name
-
使用update-rc.d命令将脚本添加到系统的启动项中
sudo update-rc.d your_script_name defaults
-
使用systemctl命令启用自启动
sudo systemctl enable your_script_name
-
重启开发板验证自启动服务程序是否运行正常
root@ubuntu:~# systemctl status your_script_name.service
● your_script_name.service - LSB: Start your_service_name at boot time
Loaded: loaded (/etc/init.d/your_script_name; generated)
Active: active (exited) since Wed 2023-04-19 15:01:12 CST; 57s ago
Docs: man:systemd-sysv-generator(8)
Process: 2768 ExecStart=/etc/init.d/your_script_name start (code=exited, status=0/SUCCESS)